package boot.spring.controller;

import boot.spring.service.WebSocketServer;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

@Controller
public class MapController {
  @Autowired
  private WebSocketServer webSocketServer;
  @RequestMapping(value="/map",method = RequestMethod.GET)
  public String openMap(){
    return "map";
  }
  @RequestMapping(value="/cutmap",method = RequestMethod.GET)
  @ResponseBody
  public String cutMap(@RequestParam("id") String id,@RequestParam("img") String img){
    JSONObject object = new JSONObject();
    if(id == null){
      object.put("to","1");
      object.put("mess","未找到房间");
      return object.toJSONString();
    }
    if (img != null){
      object.put("img",img);
    }
    object.put("to","0");
    object.put("code","1");
    webSocketServer.sendInfo(id,object.toJSONString());
    return object.toJSONString();
  }
  @RequestMapping(value="/syn",method = RequestMethod.GET)
  @ResponseBody
  public String syn(@RequestParam("id") String id,@RequestParam("me") String mePoint,
                    @RequestParam("you") String youPoint,@RequestParam(value="i",required = false) String iPoint,
                    @RequestParam(value="c4",required = false) String c4Point){
    JSONObject object = new JSONObject();
    if(id == null){
      object.put("to","1");
      object.put("mess","未找到房间");
      return object.toJSONString();
    }
    object.put("to","0");
    object.put("code","2");
    if(mePoint != null){
      String[] split = mePoint.split(";");
      if (split.length >0){
        List<String> strings = Arrays.asList(split);
        object.put("me",strings);

      }
    }
    if(youPoint != null){
      String[] youSplit = youPoint.split(";");
      if (youSplit.length >0){
        List<String> strings = Arrays.asList(youSplit);
        object.put("you",strings);

      }
    }
    if (iPoint != null){
        object.put("im",iPoint);
    }
    if (c4Point != null){
        object.put("c4",c4Point);
    }
    webSocketServer.sendInfo(id,object.toJSONString());
    return object.toJSONString();
  }
}
